package us.icitap.cm.ui.policecase;

import java.util.Arrays;

import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.Control;
import us.icitap.cm.business.entities.ControlAppEntityFound;
import us.icitap.cm.business.entities.ControlAppEntityFound.ItemIdentificationEnum;
import us.icitap.cm.business.entities.ControlAppEntityFound.ItemStatusEnum;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTable;
import us.icitap.cm.ui.custom.CustVerticalLayout;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.GeneralVars;
import us.icitap.cm.utils.HandleNotification;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.Action;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.VerticalLayout;

public class ControlAppEntityFoundDataLayout extends VerticalLayout{

	private Control control = null;
	CustTable assocTable = new CustTable();
	final BeanItemContainer<ControlAppEntityFound> beans  = new BeanItemContainer<ControlAppEntityFound>(ControlAppEntityFound.class);
	private final BeanItemContainer<AppEntity> appEntityContainer = new BeanItemContainer<AppEntity>(AppEntity.class);
	
	private final CustComboBox entitySelect = new CustComboBox("Entiteti");
	
	private CustComboBox itemIdentificationSelect = new CustComboBox("Identifikimi i objektit");
	private final CustComboBox itemStatusSelect = new CustComboBox("Statusi i objektit");
	

	private final CustomFormLayout cfl;
	
	public ControlAppEntityFoundDataLayout(String width){
		this.setMargin(true);	
		cfl = new CustomFormLayout(width, null, "Reset", "Shto");
		this.addComponent(cfl);
		
		assocTable.setContainerDataSource(beans);
		assocTable.setVisibleColumns(new Object[]{ "appEntity", "itemStatusEnum"});
		assocTable.setColumnHeaders(new String[]{ "Objektet/Personat", "Statusi"});
		assocTable.setPageLength(5);
		assocTable.setImmediate(true);

		assocTable.setSizeFull();
		
		assocTable.addActionHandler(new Action.Handler() {
			
			@Override
			public void handleAction(Action action, Object sender, Object target) {
				if (action == GeneralVars.REMOVE){
					beans.removeItem(target);
					System.out.println(target);
					control.removeControlAppEntityFound((ControlAppEntityFound) target);
				}
				
			}
			
			@Override
			public Action[] getActions(Object target, Object sender) {
				if(target == null) return null;
				 return new Action[]{GeneralVars.REMOVE};
			}
		});
		
		cfl.addComponent(assocTable);
		cfl.setComponentAlignment(assocTable, Alignment.MIDDLE_CENTER);
		CustVerticalLayout vl = new CustVerticalLayout();
		FormLayout fl = new FormLayout();
		
		fl.addComponent(this.entitySelect);
		fl.addComponent(this.itemIdentificationSelect);
		fl.addComponent(this.itemStatusSelect);

		vl.addComponent(fl);
		
		cfl.addComponent(vl);
		
		cfl.addFooter();
		cfl.displayLeftButton(false);
		
		this.entitySelect.setContainerDataSource(appEntityContainer);
		entitySelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		entitySelect.setItemCaptionPropertyId("alternativeString");
		entitySelect.setNullSelectionAllowed(false);
		entitySelect.setWidth("20em");
		
		itemIdentificationSelect.setContainerDataSource(new BeanItemContainer<ItemIdentificationEnum>(ItemIdentificationEnum.class, Arrays.asList(ItemIdentificationEnum.values())));
		itemIdentificationSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		itemIdentificationSelect.setItemCaptionPropertyId("caption");
		
		itemStatusSelect.setContainerDataSource(new BeanItemContainer<ItemStatusEnum>(ItemStatusEnum.class, Arrays.asList(ItemStatusEnum.values())));
		itemStatusSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		itemStatusSelect.setItemCaptionPropertyId("caption");
		
		
		this.itemIdentificationSelect.setVisible(false);
		this.itemStatusSelect.setVisible(false);
		
		this.entitySelect.setImmediate(true);
		this.entitySelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				isPersonSelected();
				
			}
		});
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				if(entitySelect.getValue() == null){
					HandleNotification.showWarningOnScreen("Duhet te zgjidhni nje vend/automjet/person per ta shtuar ne tabele");
					return;
				}
				
				ControlAppEntityFound controlAppEntityFound = new ControlAppEntityFound();
				controlAppEntityFound.setAppEntity((AppEntity) entitySelect.getValue());
				controlAppEntityFound.setControl(control);
				control.addControlAppEntityFound(controlAppEntityFound);
				if(itemIdentificationSelect.getValue() != null)
					controlAppEntityFound.setItemIdentificationEnum((ItemIdentificationEnum) itemIdentificationSelect.getValue());
				if(itemStatusSelect.getValue() != null)
					controlAppEntityFound.setItemStatusEnum((ItemStatusEnum) itemStatusSelect.getValue());
				
				
				
				beans.addBean(controlAppEntityFound);
				
				System.out.println(beans.size());
				resetData();
				
			}
		});

	}
	
	private void isPersonSelected(){
		if(entitySelect.getValue() != null){
			AppEntity appEntity = (AppEntity) entitySelect.getValue();
			if(appEntity.getEntityType().equals(EntityTypeEnum.PERSON)){
				itemIdentificationSelect.setVisible(false);
				this.itemIdentificationSelect.setValue(null);
				itemStatusSelect.setVisible(false);
				this.itemStatusSelect.setValue(null);
			}
			else{
				itemIdentificationSelect.setVisible(true);
				itemStatusSelect.setVisible(true);
			}
		}
		else{
			itemIdentificationSelect.setVisible(true);
			itemStatusSelect.setVisible(true);
		}
	}
	
	private void loadEntityList(Report report){
		if(report != null && report.getEntityList() != null && !report.getEntityList().isEmpty()){
			for (AppEntity appEntity : report.getEntityList()) {
				if(appEntity.getEntityType().equals(EntityTypeEnum.DOCUMENT)
					|| appEntity.getEntityType().equals(EntityTypeEnum.VEHICLE)
					|| appEntity.getEntityType().equals(EntityTypeEnum.DRUG)
					|| appEntity.getEntityType().equals(EntityTypeEnum.WEAPON)
					|| appEntity.getEntityType().equals(EntityTypeEnum.ITEM)
					|| appEntity.getEntityType().equals(EntityTypeEnum.PERSON)){
					appEntityContainer.addBean(appEntity);
				}
			}
		}
	}
	
	public void setControl(Control control, boolean editable){
		this.control = control;
		beans.removeAllItems();
		if(control.getControlAppEntityFoundList() != null)
			beans.addAll(control.getControlAppEntityFoundList());
		
		cfl.enableButtons(editable);
		loadEntityList(control.getReport());
		
		
	}
	
	private void resetData(){
		this.entitySelect.setValue(null);

		this.itemIdentificationSelect.setValue(null);
		this.itemIdentificationSelect.setVisible(false);
		this.itemStatusSelect.setValue(null);
		this.itemStatusSelect.setVisible(false);
	}
	
}
